import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: true,
    form: {
      materialCode: "",
      materialName: "",
      uniqueCode: '',
      startTime: dayjs().format('YYYY-MM-DD HH:mm:ss'), // 开始时间
      endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'), // 结束时间
      stepCode: "",
      stepName: "",
      outPut: '', // 数量
      unitType: '',
      unitTypeCode: '',
      unitTypeName: '',
      columnData: [],
      columns: [],
      dapId: '',
      dataId: ''
    },
    type: 'add'
  },
  onLoad(query) {
    if (query.type == 'add') {
      dd.setNavigationBar({
        title: "新增生产报工"
      })
      this.setData({
        delBtnShow: false
      })
    }
    if (query.type == 'edit') {
      dd.setNavigationBar({
        title: "生产报工编辑"
      })
      this.setData({
        delBtnShow: true
      })
      this.productionReportQuery(query.id);
    }
    // this.getMateDataRecordQuery()
    this.setData({
      type: query.type
    })
  },

  onShow() {
    if (this.data.getSelectTechnics) {
      console.log(this.data.selectTechnicsData)
      this.setData({
        "form.stepName": this.data.selectTechnicsData[0].title,
        "form.stepCode": this.data.selectTechnicsData[0].value
      })
    }
    if (this.data.getSelectRange) {
      console.log(this.data.selectRangeData)
      this.setData({
        "form.unitType": this.data.selectRangeData[0].level,
        "form.unitTypeCode": this.data.selectRangeData[0].value,
        "form.unitTypeName": this.data.selectRangeData[0].title
      })
    }
    // 产出物料
    if (this.data.getMaterial) {
      console.log(this.data.materialData)
      this.setData({
        "form.materialCode": this.data.materialData.code,
      })
    }
  },

  // 物料选择
  toMaterialSelection() {
    dd.navigateTo({
      url: '/pages/materialSelection/materialSelection'
    })
  },

  // 生产开始时间或结束时间选择
  selProData(e) {
    let that = this;
    let prop = e.target.dataset.prop;
    let currentDate = e.target.dataset.date;
    dd.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss',
      currentDate: currentDate,
      success: (res) => {
        if (prop == 'start') {
          that.setData({
            'form.startTime': res.date
          })
        } else {
          that.setData({
            'form.endTime': res.date
          })
        }
      },
    });
  },
  // 工艺步骤和跳转
  toSelectTechnics(e) {
    dd.navigateTo({
      url: '/pages/selectTechnics/selectTechnics?type=back'
    })
  },
  //生产单元
  toSelectRange(e) {
    dd.navigateTo({
      url: '/pages/selectRange/selectRange?type=back&noStorage=true'
    })
  },
  // 关键工艺参数
  toProductKeyParamet() {
    dd.navigateTo({
      url: '/pages/productKeyParamet/productKeyParamet'
    })
  },
  // 数量
  outPutInp(e) {
    this.data.form.outPut = e.detail.value
  },

  // 编辑页面时查询报⼯详情
  productionReportQuery(id) {
    dd.showLoading({
      content: '载入中...',
    });
    let that = this
    $http(apiSetting.productionReportQuery, {
      reportId: id, //⽣产报⼯ID
    }).then((data) => {
      if (data.code == 0) {
        let newObj = data.result
        if (newObj.recordDTO) {
          newObj.dapId = newObj.recordDTO.dapId
          newObj.dataId = newObj.recordDTO.dataId
          let arr1 = []
          let arr2 = []
          for (let i in newObj.recordDTO.columns) {
            if (newObj.recordDTO.columns[i] == 'dap_id_') {
              break
            } else {
              if (newObj.recordDTO.columnData[i] != '') {
                arr1.push(newObj.recordDTO.columnData[i])
                arr2.push(newObj.recordDTO.columns[i])
              }
            }
          }
          newObj.columnData = arr1
          newObj.columns = arr2
        }
        console.log(newObj)
        that.setData({
          form: newObj
        })
      }
      dd.hideLoading();
    })
  },
  // 保存
  save() {
    let that = this;
    if (that.data.type == 'add') {
      $http(apiSetting.productionReportAdd, {
        materialCode: that.data.form.materialCode, //物料主数据编码
        uniqueCode: that.data.form.uniqueCode,
        startTime: dayjs(that.data.form.startTime).format('YYYY-MM-DD HH:mm:ss'), // 开始时间
        endTime: dayjs(that.data.form.endTime).format('YYYY-MM-DD HH:mm:ss'), // 结束时间
        stepCode: that.data.form.stepCode,
        outPut: that.data.form.outPut, // 数量
        unitType: that.data.form.unitType,
        unitTypeCode: that.data.form.unitTypeCode,
        columnData: that.data.form.columnData,
        columns: that.data.form.columns,
        dapId: that.data.form.dapId,
        dataId: that.data.form.dataId,
      }).then((data) => {
        if (data.code == 0) {
          dd.navigateBack({
            delta: 1
          })
        }
      })
    } else {
      // 更新保存
      $http(apiSetting.productionReportUpdate, {
        materialCode: that.data.form.materialCode, //物料主数据编码
        uniqueCode: that.data.form.uniqueCode,
        startTime: dayjs(that.data.form.startTime).format('YYYY-MM-DD HH:mm:ss'), // 开始时间
        endTime: dayjs(that.data.form.endTime).format('YYYY-MM-DD HH:mm:ss'), // 结束时间
        stepCode: that.data.form.stepCode,
        outPut: that.data.form.outPut, // 数量
        unitType: that.data.form.unitType,
        unitTypeCode: that.data.form.unitTypeCode,
        columnData: that.data.form.columnData,
        columns: that.data.form.columns,
        dapId: that.data.form.dapId,
        dataId: that.data.form.dataId,
        id: that.data.form.id, //⽣产报⼯Id  ---------------与新增的区别之处----------------
      }).then((data) => {
        if (data.code == 0) {
          dd.navigateBack({
            delta: 1
          })
        }
      })
    }
  },
  // 删除
  delete() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm == true) {
          $http(apiSetting.productionReportDelete, {
            reportIds: [that.data.form.id], //⽣产报⼯ID
          }).then((data) => {
            if (data.code == 0) {
              dd.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
    });
  },

  uniqueCodeScan() {
    let that = this;
    dd.scan({
      // qr:二维码,bar:条形码
      type: 'qr',
      success: (res) => {
        that.setData({
          'form.uniqueCode': res.code
        })
      },
    });
  },

  uniqueCodeInp(e) {
    this.data.form.uniqueCode = e.detail.value
  },
});
